/**
 * 首页数据状态管理,线上接口
 */
import utils  from '../uview-ui/index.js'
import api from '../api/index.js'
const module = {
  namespaced: true,
  state:()=>({
	baselist:[
		// {
		// 	price: 35,
		// 	title: '北国风光，千里冰封，万里雪飘',
		// 	shop: '李白杜甫白居易旗舰店',
		// 	image: 'http://pic.sc.chinaz.com/Files/pic/pic9/202002/zzpic23327_s.jpg',
		// },
		// {
		// 	price: 75,
		// 	title: '望长城内外，惟余莽莽',
		// 	shop: '李白杜甫白居易旗舰店',
		// 	image: 'http://pic.sc.chinaz.com/Files/pic/pic9/202002/zzpic23325_s.jpg',
		// },
		// {
		// 	price: 385,
		// 	title: '大河上下，顿失滔滔',
		// 	shop: '李白杜甫白居易旗舰店',
		// 	image: 'http://pic2.sc.chinaz.com/Files/pic/pic9/202002/hpic2119_s.jpg',
		// },
		// {
		// 	price: 784,
		// 	title: '欲与天公试比高',
		// 	shop: '李白杜甫白居易旗舰店',
		// 	image: 'http://pic2.sc.chinaz.com/Files/pic/pic9/202002/zzpic23369_s.jpg',
		// },
	],
    list:[],//首页加载列表数据
	loadStatus:'loadmore',
  }),
  mutations: {
	refreshList(state){
		console.log("refreshList")
		state.list = [];
	},
    getList(state,data) {
		if(data && data.length){
			data.forEach((item,index)=>{
				item.id = utils.$u.guid();
				item.image = `https://picsum.photos/300?random=${index}`
			})
			state.list = [...state.list,...data];
		}
		console.log("state.list",state);
		// 这里的 `state` 对象是模块的局部状态
    }
  },
  actions:{
	  // state为当前模块的数据，commit触发mutation方法，rootState是根节点状态，info是传参
	getList({state,commit,rootState},){
		state.loadStatus = 'loading';
		if(uni.getStorageSync('city')){
			api.loadbyname({
				name:uni.getStorageSync('city')
			}).then(res=>{
				console.log("recommendcity",res);
				state.loadStatus = 'loadmore';
				commit('getList',res.data.data);
			})
		}
	},
	refreshList({state,commit,rootState},info){
		console.log("refreshList")
		commit('refreshList')
		state.loadStatus = 'loading';
		setTimeout(() => {
			if(uni.getStorageSync('city')){
				api.loadbyname({
					name:uni.getStorageSync('city')
				}).then(res=>{
					console.log("recommendcity",res);
					state.loadStatus = 'loadmore';
					commit('getList',res.data.data);
				})
			}
			state.loadStatus = 'loadmore';
		}, 1000)		
	}
  },
  getters: {
    getloadStatus(state){
      return state.loadStatus;
    },
	getList(state){
	  return state.list
	}
  }
}
export default module